package com.umeox.watch.moto.dataservice.stub;

import android.os.IBinder;
import android.os.RemoteException;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.interfaces.IPair;
import com.umeox.watch.moto.dataservice.services.ApiService;

import net.vidageek.mirror.dsl.Mirror;

import java.util.Arrays;

public class PairStub extends android.os.Binder implements IPair {
    public PairStub() {
        this.attachInterface(this, DESCRIPTOR);
    }

    public static void registerBinder() {
        try {
            new Mirror().on("android.os.ServiceManager").invoke().method("addService").withArgs(BuildConfig.pairBinderName, new PairStub());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder asBinder() {
        return this;
    }

    @Override
    public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws RemoteException {
        String descriptor = DESCRIPTOR;
        switch (code) {
            case INTERFACE_TRANSACTION: {
                reply.writeString(descriptor);
                return true;
            }
            case TRANSACTION_MAKEFRIENDS: {
                data.enforceInterface(descriptor);
                String selfMac = data.readString();
                String[] nearByMacs = data.createStringArray();
                int result = makeFriends(selfMac, nearByMacs);
                reply.writeNoException();
                reply.writeInt(result);
                return true;
            }
            default: {
                return super.onTransact(code, data, reply, flags);
            }
        }
    }

    @Override
    public int makeFriends(String selfMac, String[] nearByMacs) {
        Logger.i("请求交友：" + Arrays.toString(nearByMacs));
        return ApiService.makeFriends(selfMac, nearByMacs);
    }
}
